﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Patchs
{
    public class patchs
    {
        public delegate void updateCaller(string str);

        public static bool Patchs(eObj.organisation org, updateCaller upd_caller)
        {
            patchs p = new patchs();

            if (org.patch_level<1 && p._patch1(org, upd_caller) == false)
                return false;
            if (org.patch_level<2 && p._patch2(org, upd_caller) == false)
                return false;
            if (org.patch_level<3 && p._patch3(org, upd_caller) == false)
                return false;
            return true;
        }

        /// <summary>
        /// Patch to add api id to employee
        /// </summary>
        /// <param name="org">Organisation</param>
        /// <param name="upd_caller">CallerHandler</param>
        /// <returns>true=ok,false=ko</returns>
        private bool _patch1(eObj.organisation org, updateCaller upd_caller)
        {
            eApi.api _api = new eApi.api();

            foreach (eObj.company c in org.company_list)
            {
                foreach (eObj.employee e in c.employee_list)
                {
                    if ((e.api_id == null || e.api_id == "") && e.deleted == false)
                    {
                        upd_caller("updating api id for " + e.name);
                        eApi.apiEmployee _apiE = _api.loadCitizenFromWeb("", e.name);
                        if (_apiE != null)
                            e.api_id = _apiE.id;
                        else
                        {
                            if (e.date_out == null)
                            {
                                e.locked = true;
                                MessageBox.Show(e.name + " may has a bad name. You must change it in employee tab", c.name + " - Bad name for " + e.name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            else
                            {
                                string str = e.name;
                                if (Tools.InputBox(c.name + " - Bad name for " + e.name, e.name + " may has a bad name. Please enter the good one or cancel to delete him", ref str) == DialogResult.OK)
                                {
                                    e.name = str;
                                }
                                else
                                    e.deleted = true;
                            }
                        }
                    }
                }
            }
            org.patch_level = 1;
            return true;
        }

        /// <summary>
        /// Patch to add org owner to all companies
        /// </summary>
        /// <param name="org">Organisation Objet</param>
        /// <param name="upd_caller">CallerHandler</param>
        /// <returns>true=ok,false=ko</returns>
        private bool _patch2(eObj.organisation org, updateCaller upd_caller)
        {
            if (org.api_id == null || org.api_id == "")
            {
                string new_api_id = "";

                //Getting user id if we don't have it
                if (Tools.InputBox("eAPI - " + org.name + " user ID", "We don't have your user api ID. Please enter it :", ref new_api_id) != DialogResult.OK)
                    return false;

                org.api_id = new_api_id;
            }
            else
            {
                org.patch_level = 2;
                return true;
            }
   
            eManager.eManager eM = new eManager.eManager();

            eApi.api _api = new eApi.api();

            upd_caller("getting user profile from eApi");
            eApi.apiEmployee api_emp = _api.loadCitizenFromWeb(org.api_id,"");
            if (api_emp == null)
            {
                return false;
            }

            foreach (eObj.company c in org.company_list)
            {
                eObj.employee emp = eM.getEmployeeByApiId(c,org.api_id, false);

                if (emp == null) //Employee don't exists in company
                {
                    //creating new employee
                    upd_caller("adding user "+ api_emp.name +" to company " + c.name);
                    emp = new eObj.employee();
                    emp.id = Guid.NewGuid().ToString();
                    emp.api_id = org.api_id;
                    emp.name = api_emp.name;
                    emp.fireable = false;
                    emp.date_in = DateTime.Now;
                    eObj.salary sal = new eObj.salary();
                    sal.date = DateTime.Now;
                    sal.value = 0;

                    c.employee_list.Add(emp);
                }
                else //Employee already exists in company
                {
                    //updating current employee
                    upd_caller("updating " + c.name + " company user " + emp.name);
                    emp.date_out = null; //if employee was fired
                    emp.fireable = false; //setting employee to not fireable
                    eObj.salary sal = new eObj.salary();
                    sal.date = DateTime.Now;
                    sal.value = 0;
                    emp.salary_list.Add(sal); //setting new salary
                }
            }
            org.patch_level = 2;
            return true;
        }

        /// <summary>
        /// Patch to add customization level to each company
        /// </summary>
        /// <param name="org">Organisation</param>
        /// <param name="upd_caller">CallerHandler</param>
        /// <returns>true=ok,false=ko</returns>
        private bool _patch3(eObj.organisation org, updateCaller upd_caller)
        {
            eApi.api _api = new eApi.api();

            foreach (eObj.company c in org.company_list)
            {
                if (c.api_id == null || c.api_id == "")
                {
                    string new_api_id = "";

                    //Getting company id if we don't have it
                    if (Tools.InputBox("eAPI - " + c.name + " company ID", "We don't have your company api ID. Please enter it :", ref new_api_id) != DialogResult.OK)
                        return false;

                    c.api_id = new_api_id;
                }

                upd_caller("getting company "+ c.name +" from eApi");
                eObj.company _c = _api.loadCompanyFromWeb(c.api_id, null);
                if (_c == null)
                {
                    MessageBox.Show("Company not found.\nCompany api Id may be bad.", "eAPI - " + c.name + " company ID", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

                c.customized_level = _c.customized_level;
            }

            org.patch_level = 3;
            return true;
        }
    }
}
